package com.tanhua.dubbo.api;


import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.Blossom;
import com.tanhua.model.mongo.BlossomReceive;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class BlossomApiImpl implements BlossomApi{
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public Integer findCount(Blossom blossom) {
        //只能发送三条数据
        Criteria criteria=Criteria.where("userId").is(blossom.getUserId()).and
                ("date").is(blossom.getDate());
        Query query=Query.query(criteria);
        long count = mongoTemplate.count(query, Blossom.class);
        Criteria criteria1=Criteria.where("userId").is(blossom.getUserId()).and
                ("date").is(blossom.getDate());
        Query query1=Query.query(criteria);
        long count1 = mongoTemplate.count(query, BlossomReceive.class);
        //已经存进去一条了
        //mongoTemplate.save(blossom);
        return (int)count+(int)count1;
    }

    @Override
    public Integer findReceiveCount(Blossom blossom) {
        Criteria criteria1=Criteria.where("toUserId").is(blossom.getUserId()).and
                ("date").is(blossom.getDate());
        Query query1=Query.query(criteria1);
        long count1 = mongoTemplate.count(query1, BlossomReceive.class);
        return (int)count1;
    }

    @Override
    public void save(Blossom blossom) {
        mongoTemplate.save(blossom);
    }

    @Override
    public Map Random(Blossom blossom) {
//        Criteria criteria1=Criteria.where("toUserId").is(blossom.getUserId()).and
        //("date").is(blossom.getDate());
//        Query query1=Query.query(criteria1);
//        long count1 = mongoTemplate.count(query1, BlossomReceive.class);
//
        Integer count1 = findReceiveCount(blossom);
        Integer remainingTimes=3-count1;
//随机筛选一条不是自己的数据
        Criteria criteria=Criteria.where("userId").nin(blossom.getUserId());
        TypedAggregation<Blossom> newAggregation = TypedAggregation.newAggregation
                (Blossom.class,
                        Aggregation.match(criteria),
                        Aggregation.sample(1)
                );
        AggregationResults<Blossom> results = mongoTemplate.aggregate(newAggregation,
                Blossom.class);
        Map map=new HashMap();
        List<Blossom> mappedResults = results.getMappedResults();
        //得到的就是这一条数据，只有一个值来接收；
        if(mappedResults!=null){
            Long userId=null;
            String soundUrl=null;
            for (Blossom mappedResult : mappedResults) {
                userId = mappedResult.getUserId();
                soundUrl = mappedResult.getSoundUrl();
            }
            List<ObjectId> id = CollUtil.getFieldValues(mappedResults, "id", ObjectId.class);
            Query query=Query.query(Criteria.where("id").in(id));
            mongoTemplate.remove(query,Blossom.class);
            //根据类型来的保存到收到的表中
            for (Blossom mappedResult : mappedResults) {
                BlossomReceive blossomReceive=new BlossomReceive();
                blossomReceive.setToUserId(blossom.getUserId());
                blossomReceive.setUserId(mappedResult.getUserId());
                blossomReceive.setDate(mappedResult.getDate());
                blossomReceive.setSoundUrl(mappedResult.getSoundUrl());
                mongoTemplate.save(blossomReceive);
            }

            //Map map=new HashMap();
            map.put("remainingTimes",remainingTimes);
            map.put("userId",userId);
            map.put("soundUrl",soundUrl);
            return map;

        }else{
            //Map map=new HashMap();
            map.put("userId",-1L);
        }
        return map;
    }
}
